import jdk.swing.interop.SwingInterOpUtils;

import java.util.Locale;

class Student {
    private String name;
    private int age;

    public Student(String name,int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Test {
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("hello");
        stringBuilder.append(" word");
        System.out.println("===============");
        StringBuffer stringBuffer = new StringBuffer("abcde");
        stringBuffer.append( "fghl");
        System.out.println(stringBuffer);
    }
    public static void main21(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("hello");
        System.out.println("反转前：" +stringBuilder);
        stringBuilder.reverse();
        System.out.println("反转后：" +stringBuilder);
    }
    public static void main20(String[] args) {
        StringBuilder stringBuilder = new StringBuilder("hello");
        //在尾部追加，相当于String的+=
        stringBuilder.append(" word");
        System.out.println(stringBuilder);
        System.out.println("=============");
        //在offset位置插入
        stringBuilder.insert(2," word ");
        System.out.println(stringBuilder);
        System.out.println("=============");
        //删除某位置字符
        stringBuilder.deleteCharAt(0);
        System.out.println(stringBuilder);
        System.out.println("=============");
        //删除删除某位置到某位置
        stringBuilder.delete(0,2);
        System.out.println(stringBuilder);
        System.out.println("=============");
        //从某位置到某位置替换成某个String字符串
        stringBuilder.replace(0,3,"abcd");
        System.out.println(stringBuilder);
    }
    public static void main19(String[] args) {
        //对于字符串的修改：
        String str = "hello";
        str += " word";
        System.out.println(str);
//        final int[] ch = {1,2,3,4,5,6};
//        ch[0] = 100;

    }
    public static void main18(String[] args) {
        //去掉字符串中的左右空格,保留中间空格
        String str ="  hello word   ";
        System.out.println(str);
        String s = str.trim();
        System.out.println(s);
    }
    public static void main17(String[] args) {
        //字符串的截取：
        String str = "hello word";
        String s1 = str.substring(3);//从3下标开始到末尾
        System.out.println(s1);
        System.out.println("============");
        String s2 = str.substring(3,8);//从指定位置到指定位置
        System.out.println(s2);
    }
    public static void main16(String[] args) {
        //如果一个字符串中有多个分隔符，可以用"|"作为连字符
        String str = "abcdef=dadhaj&dajkja=ada";
        String[] newstr =str.split("=|&");
        for(String s : newstr) {
            System.out.println(s);
        }
    }
    public static void main15(String[] args) {
        //特殊的符号需要在前面加上"\\",比如"." "+" "*" "|"等字符
        String str = "2004.03.16";
        String[] newstr = str.split("\\.");
        for (String s : newstr) {
            System.out.println(s);
        }
        System.out.println("=================");
        //对于"\"我们需要分割符为"\\\\"
        String str2 = "ab\\cd\\ef\\";
        System.out.println(str2);
        String[] newstr2 = str2.split("\\\\");
        for (String s : newstr2) {
            System.out.println(s);
        }
    }
    public static void main14(String[] args) {
        //按照指定的分割符进行拆分
        String str4 = new String("abcg=sdhah&da=dad&da");
        String[] newstr = str4.split("=");
        for (int i = 0; i < newstr.length; i++) {
            String[] newstr2 = newstr[i].split("&");
            for (int j = 0; j < newstr2.length; j++) {
                System.out.println(newstr2[j]);
            }
        }
        System.out.println("===============");
        String str = new String("hello hello w o r d");
        String[] str2 = str.split(" ");
        for (String s : str2) {
            System.out.println(s);
        }
        System.out.println("=================");
        //将字符串按照指定的分割符拆分成几组
        String[] str3 = str.split(" ",2);
        for(String s : str3) {
            System.out.println(s);
        }
    }
    public static void main13(String[] args) {
        //把某个字符替换成新的字符
        String oldstr = new String("hello hello word");
        String newstr1 = oldstr.replace('o','k');
        System.out.println(newstr1);
        System.out.println("=============");
        String newstr2 = oldstr.replace("ll","wwww");
        System.out.println(newstr2);
        System.out.println("=============");
        String newstr3 = oldstr.replaceFirst("ll","aaaa");
        System.out.println(newstr3);
        System.out.println("=============");
        String newstr4 = oldstr.replaceAll("ll","aaaa");
        System.out.println(newstr4);
    }
    public static void main12(String[] args) {
        //格式化
        String str = String.format("%d/%d/%d",2024,8,14);
        System.out.println(str);
    }
    public static void main11(String[] args) {
        //字符串转换成数组
        String str = new String("hello word");
        char[] ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            System.out.println(ch[i]);
        }
        System.out.println("=============");
        //数组转换成字符串
        String str2 = new String(ch);
        System.out.println(str2);
    }
    public static void main10(String[] args) {
        //大小写之间的转换
        String str1 = new String("hello word");
        String str2 = new String("ABCDEFG");
        System.out.println(str1.toUpperCase());//小写转换成大写
        System.out.println(str2.toLowerCase());//大写转换成小写
    }
    public static void main9(String[] args) {
        //字符串转换为数值
        String str1 = new String("1234");
        String str2 = new String("1.234");
        int intstr1 = Integer.parseInt(str1);
        double doustr2 = Double.parseDouble(str2);
        System.out.println(intstr1);
        System.out.println(doustr2);
    }
    public static void main8(String[] args) {
        //把数值转换成字符串。
        String str1 = String.valueOf(123);
        String str2 = String.valueOf(1.23);
        String str3 = String.valueOf(true);
        String str4 = String.valueOf(new Student("lisi",25));
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println(str4);
    }
    public static void main7(String[] args) {
        String str = "helloword";
        int index = str.indexOf('l');
        System.out.println(index);

        int lastindex = str.lastIndexOf('l');
        System.out.println(lastindex);
        int indexfrom = str.indexOf('l',3);
        System.out.println(indexfrom);

        int lastindexfrom = str.indexOf('l',3);
        System.out.println(lastindexfrom);

        int indexString = str.indexOf("ll");
        System.out.println(indexString);

        int lastindexString = str.lastIndexOf("ll");
        System.out.println(lastindexString);

        int indexStringfrom = str.indexOf("ll",3);
        System.out.println(indexStringfrom);

        int lastindexStringfrom = str.lastIndexOf("ll",3);
        System.out.println(lastindexStringfrom);

//        //char c = str.charAt(-1);//会报错
//        for (int i = 0; i < str.length(); i++) {
//            char ch = str.charAt(i);
//            System.out.println(ch);
//        }
    }
    public static void main6(String[] args) {
        //对于compareTo方法，按照两个规则比较
        //1、按照字典的顺序比较，如果出现不等的字符，那么返回他们的差值。
        //2、如果比较的前k个相同，也就是对于短的字符串比较完了，而长的还没比较完，那么返回它们的长度的差值
        String s1 = new String("abcd");
        String s2 = new String("dabc");
        String s3 = new String("abcd");
        String s4 = new String("abcdefgh");
        System.out.println(s1.compareTo(s2));//返回a和d的差值
        System.out.println(s1.compareTo(s3));//相同返回0
        System.out.println(s1.compareTo(s4));//返回s1和s4的长度之间的差值
        String s5 = new String("Abcd");
        System.out.println(s1.compareTo(s5));
        System.out.println(s1.compareToIgnoreCase(s5));//忽略大小写比较
    }
    public static void main5(String[] args) {
        String str1 = new String("hello");
        String str2 = new String("hello");
        String str3 = new String("word");
        String str4 = str1;
        System.out.println(str1 == str2);//false
        System.out.println(str1 == str3);//false
        System.out.println(str1 == str4);//true

        System.out.println("===================");

        System.out.println(str1.equals(str2));//true
        System.out.println(str1.equals(str3));//false
        System.out.println(str1.equals(str4));//true
    }
    public static void main4(String[] args) {
        int a = 5;
        int b = 10;
        int c = a;
        System.out.println(a == b);//false
        System.out.println(a == c);//true

        System.out.println("================");

        String str1 = new String("hello");
        String str2 = new String("hello");
        String str3 = new String("word");
        String str4 = str1;
        System.out.println(str1 == str2);//false
        System.out.println(str1 == str3);//false
        System.out.println(str1 == str4);//true
    }
    public static void main3(String[] args) {
        //"hello"是String类型
        System.out.println("hello".length());
    }
    public static void main2(String[] args) {
        String s1 = "hello";
        String s2 = "word";
        String s3 = s1;
        System.out.println(s1.length());
        System.out.println(s1.isEmpty());//长度是否是0，为零返回true，不为零返回false
    }
    public static void main1(String[] args) {
        //1.直接使用字符串常量来构造
        String str1 = "hello word";
        System.out.println(str1);

        //2.使用new String对象来进行初始化
        String str2 = new String("hello word");
        System.out.println(str2);

        //3.使用字符数组进行构造
        char[] array = {'h','e','l','l','o',' ','w','o','r','d'};
        String str3 = new String(array);
        System.out.println(str3);
    }
}
